// popup_dlg.js
var Gcommon = require('./../../model/Common');
cc.Class({
  extends: cc.Component,

  properties: {
    // foo: {
    //    default: null,      // The default value will be used only when the component attaching
    //                           to a node for the first time
    //    url: cc.Texture2D,  // optional, default is typeof default
    //    serializable: true, // optional, default is true
    //    visible: true,      // optional, default is true
    //    displayName: 'Foo', // optional
    //    readonly: false,    // optional, default is false
    // },
    // ...
    mask: {
      type: cc.Node,
      default: null
    },

    mask_opacity: 128,

    content: {
      type: cc.Node,
      default: null
    },
    callBack: null
  },

  // use this for initialization
  onLoad: function() {
    this.setBGM();
    this.setMusic();
  },

  show_dlg: function(cb) {
    //接收一个隐藏弹窗的回调函数
    this.callBack = cb;
    this.node.active = true;
    // mask 渐变出来;
    this.mask.opacity = 0;
    var fin = cc.fadeTo(0.3, this.mask_opacity);
    this.mask.runAction(fin);

    // dlg由小到大
    this.content.scale = 0;
    // var s = cc.scaleTo(0.4, 1).easing(cc.easeBackOut());
    // this.content.runAction(s);
    var sca = cc.scaleTo(0.3, 1).easing(cc.easeIn(0.1));
    this.content.runAction(sca);
  },

  hide_dlg: function() {
    var fout = cc.fadeOut(0.3);
    this.mask.runAction(fout);

    var s = cc.scaleTo(0.3, 0).easing(cc.easeBackIn());
    var end_func = cc.callFunc(
      function() {
        this.callBack.hide();
        this.node.active = false;
        this.node.destroy();
      }.bind(this)
    );
    var seq = cc.sequence([s, end_func]);
    this.content.runAction(seq);
  },

  //分享好友
  onShare() {
    console.log('游戏内分享好友');
    this.HomeBase.share();
  },

  // 回到首页
  returnHome() {
    cc.director.loadScene('home');
  },

  // 重新开始
  reStart() {
    this.callBack.restart();
    this.hide_dlg();
  },

  // 控制背景音乐
  changeBGM() {
    Gcommon.BGM = !Gcommon.BGM;
    this.setBGM();
  },
  setBGM() {
    let sprite_icon_res = '';
    let icon_res = ['music/music', 'music/dismusic'],
      volume = 0;

    if (Gcommon.BGM) {
      volume = 1.0;
      sprite_icon_res = icon_res[0];
    } else {
      volume = 0.0;
      sprite_icon_res = icon_res[1];
    }

    cc.audioEngine.setMusicVolume(volume);

    cc.loader.loadRes(
      sprite_icon_res,
      cc.SpriteFrame,
      function(err, ret) {
        if (err) {
          console.log(err);
          return;
        }
        // this.content.getChildByName('music').spriteFrame = ret;
        cc
          .find('music', this.content)
          .getComponent(cc.Sprite).spriteFrame = ret;
      }.bind(this)
    );
  },
  // 控制游戏音效
  changeMusic() {
    Gcommon.musicEffect = !Gcommon.musicEffect;
    this.setMusic();
  },
  setMusic() {
    let sprite_icon_res = '';
    let icon_res = ['music/yl', 'music/disyl'],
      volume = 0;

    if (Gcommon.musicEffect) {
      volume = 1.0;
      sprite_icon_res = icon_res[0];
    } else {
      volume = 0.0;
      sprite_icon_res = icon_res[1];
    }

    cc.audioEngine.setEffectsVolume(volume);

    cc.loader.loadRes(
      sprite_icon_res,
      cc.SpriteFrame,
      function(err, ret) {
        if (err) {
          console.log(err);
          return;
        }
        cc
          .find('musicEffect', this.content)
          .getComponent(cc.Sprite).spriteFrame = ret;
      }.bind(this)
    );
  }
});
